package com.shuoyue.ycgk.ui.recruitment.newsinfo;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentAdapter;
import com.shuoyue.ycgk.entity.RecruitmentItem;
import com.shuoyue.ycgk.ui.recruitment.FocusChangeContract;
import com.shuoyue.ycgk.utils.ShareUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RecurimentInfoActivity extends BaseMvpActivity<RecurimentInfoContract.Presenter>
        implements RecurimentInfoContract.View, FocusChangeContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.nameAndTime)
    TextView nameAndTime;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.lay_pager)
    LinearLayout layPager;
    @BindView(R.id.collection)
    ImageView collection;


    int id;

    IndexFragmentAdapter indexFragmentAdapter;
    List<BaseMvpFragment> fragmentMainItems;
    FocusChangeContract.Presenter focusPresenter;
    RecruitmentItem recurimentInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_news_info2;
    }

    @Override
    protected void initView() {
        pageTitle.setText("公告");
    }

    @Override
    public void initData() {
        super.initData();
        id = getIntent().getIntExtra("id", -1);
        mPresenter = new RecurimentInfoContract.Presenter();
        mPresenter.attachView(this);
        focusPresenter = new FocusChangeContract.Presenter();
        focusPresenter.attachView(this);
        mPresenter.getRecurimentInfo(id);
    }


    @Override
    public void setRecurimentInfo(RecruitmentItem recurimentInfo) {
        this.recurimentInfo = recurimentInfo;
        title.setText(recurimentInfo.getTitle());
        nameAndTime.setText((TextUtils.isEmpty(recurimentInfo.getPushUnit())
                ? "" : (recurimentInfo.getPushUnit() + " . ")) +

                recurimentInfo.getCreateTime());

        fragmentMainItems = new ArrayList<>();
        fragmentMainItems.add(FragmentNoticeDetail.getInstance(recurimentInfo));
        fragmentMainItems.add(FragmentNoticeInfoJobs.getInstance(recurimentInfo.getId()));
        fragmentMainItems.add(FragmentNoticeInfoNotice.getInstance(recurimentInfo.getExaminationPublishDTO()));
        indexFragmentAdapter = new IndexFragmentAdapter(getSupportFragmentManager(),
                fragmentMainItems);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(indexFragmentAdapter);
        viewPager.setCurrentItem(0);

        collection.setImageDrawable(ContextCompat.getDrawable(mContext,
                recurimentInfo.getIsCollect() == 1 ? R.mipmap.collection_y :
                        R.mipmap.collection_n));
    }


    @OnClick({R.id.share, R.id.collection, R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.share:
                if (recurimentInfo != null)
                    ShareUtil.shareUrl(this, ShareUtil.RECURIMENT_URL + id,
                            recurimentInfo.getTitle(),
                            "",
                            null);
                break;
            case R.id.collection:
                if (recurimentInfo != null)
                    focusPresenter.collectionNotice(recurimentInfo.getId(),
                            1,
                            recurimentInfo.getIsCollect() == 1);
                break;
        }
    }

    @Override
    public void success(boolean isFocuse) {
        collection.setImageDrawable(ContextCompat.getDrawable(mContext,
                isFocuse ? R.mipmap.collection_y :
                        R.mipmap.collection_n));
    }

    @Override
    public void enableCollectButton() {
        collection.setEnabled(true);
    }

    @Override
    public void disAbleCollectButton() {
        collection.setEnabled(false);

    }
}
